%% ========================================================================
%  MASTER SCRIPT — Run Full Project Pipeline
%
%  Author: Gaurab Aryal, Federico Zincenko (2024)
%  Last modified   : 07-21-2025
%
%  Description:
%  This script serves as the main driver to execute the entire data 
%  pipeline including loading, cleaning, analysis, visualization, and 
%  exporting results. It is designed to be run from any environment 
%  without manually changing paths.
%
%  Dependencies:
%  - Global Optimization Toolbox
%  - Statistics and Machine Learning Toolbox
%  - Parallel Computing Toolbox
%  - MIDACO Unlimited version
%
%  Usage:
%  Simply run this file. It will automatically detect the base directory
%  and execute each stage in the pipeline in sequence.
%  
%  ========================================================================

clear
clc

%% Set working directory

thisFile = mfilename('fullpath');
thisDir = fileparts(thisFile);
cd(fileparts(thisDir));

%% Step 1: Clean the data and convert data into .mat file

% The cleaned data is saved in /data/processed/OilData.mat

run('code/1_analysis/f1_cleaning.m');

%% Step 2: Data presentation.

% This code produces Table 1, Figures 1,2 in Section 4, and Figure 4 in 
% Section 6. The table and figures are saved in /output/tables_and_figures

run('code/1_analysis/f2_data_analysis.m');

%% Step 3: Estimation

% This code produces the MLE estimates used in Table 3. They are saved in 
% /output/estimates/ESTIMATES.mat

% Note: This code requires MIDACO unlimited version. Go to 
% code/2_estimation/estimate_fun_midaco.m and use your unlimited key for
% the code to run. The free version only allows up to 4 variables. Put all
% relevant MIDACO files into /2_estimation/f4_estimating_parameters/midaco

% To use the PPI for oil and gas drilling as deflator, change deflator = 2
% in f3_estimating_parameters.m

run('code/2_estimation/f3_estimating_parameters.m');

%% Step 3.5: Run bootstrap to get the confidence intervals

% Note: If you want to do this on clusters and if it supports SLURM you can
% adapt the file '/code/3_inference/bootstrap_se.sl' that we used.

addpath('code/3_inference');
nJobs = 150;
% Detect number of physical CPU cores
numCores = feature('numcores');
numWorkers = max(1, numCores - 1);  % ensure at least 1
% Start or reconfigure parallel pool
poolobj = gcp('nocreate');  % If no pool, do not create new one yet
if isempty(poolobj) || poolobj.NumWorkers ~= numWorkers
    delete(poolobj);  % Close old pool if needed
    parpool('local', numWorkers);
end
% Run BS_CI in parallel
parfor bs = 1:nJobs
    try
        theta_star_hat = BS_CI(bs);
    catch ME
        warning('Error in BS_CI(%d): %s\n', bs, ME.message);
    end
end
% Shut down pool after done
delete(gcp('nocreate'));

%% Step 4: 

% This file saves the confidence intervals from bootstrapping to 
% output/tables_and_figures/CI.mat, and produces Figure 5 saved in 
% /output/tables_and_figures

run('code/4_counterfactual/after_estimation.m');

%% Step 5: Counterfactual 

% The saved file /output/estimates/CF_results.mat calculates the mean DWL

run('code/4_counterfactual/counterfactual.m');